package com.swgk.core.base.model.factory;

import androidx.annotation.NonNull;

import com.swgk.core.BuildConfig;
import com.swgk.core.util.MLog;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BaseUrlInterceptor implements Interceptor {

    public static final String HEADERS_URL = "url:";
    public static final String HEADERS_URL_HOME = HEADERS_URL + "home"; //首页
    public static final String HEADERS_URL_MEMBER = HEADERS_URL + "member"; // 会员
    public static final String HEADERS_URL_SG = HEADERS_URL + "sg"; // 商机
    public static final String HEADERS_URL_JC = HEADERS_URL + "jc"; // jc
    public static final String HEADERS_URL_ZC = HEADERS_URL + "zc"; // 招采
    public static final String HEADERS_URL_ZL = HEADERS_URL + "zl"; // 租赁
    public static final String HEADERS_URL_USER = HEADERS_URL + "user";//用户中心
    public static final String HEADERS_URL_JY = HEADERS_URL + "jy";//企业经营
    public static final String HEADERS_URL_FW = HEADERS_URL + "fw";//企业服务
    public static final String HEADERS_URL_IM = HEADERS_URL + "im";//IM
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        Request.Builder requestBuilder = getMemberRequestBuilder(request, builder);
        MLog.d("最终使用域名 VS BASE_URL","\n最终使用域名："+request.url().toString() + "\nBASE_URL:" + BuildConfig.BASE_URL);
        if (requestBuilder != null) {
            return chain.proceed(requestBuilder.build());
        } else {
            return chain.proceed(request);
        }
    }

    private Request.Builder getMemberRequestBuilder(Request request, Request.Builder builder) {
        String[] urlMember = HEADERS_URL.split(":");
        List<String> urlHeaders = request.headers(urlMember[0]);
        if (urlHeaders.size() > 0) {
            String oldUrl = request.url().toString();
            for (String urlHeader : urlHeaders) {
                builder.removeHeader(urlMember[0]);
                if (urlHeader.equals(HEADERS_URL_MEMBER.split(":")[1])) {
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                } else
                    if (urlHeader.equals(HEADERS_URL_SG.split(":")[1])) {
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                } else if (urlHeader.equals(HEADERS_URL_ZC.split(":")[1])) {
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                }else if (urlHeader.equals(HEADERS_URL_JC.split(":")[1])) {
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                }else if(urlHeader.equals(HEADERS_URL_ZL.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                } else if (urlHeader.equals(HEADERS_URL_USER.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_USER));
                }else if (urlHeader.equals(HEADERS_URL_JY.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                }else if (urlHeader.equals(HEADERS_URL_HOME.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_HY));
                }else if (urlHeader.equals(HEADERS_URL_IM.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_IM));
                }else if (urlHeader.equals(HEADERS_URL_FW.split(":")[1])){
                    builder.url(oldUrl.replace(BuildConfig.BASE_URL, BuildConfig.BASE_URL_FW));
                }
            }
            return builder;
        }
        return null;
    }

    private String getType(String bindUrl){
        String type = "debug : " + bindUrl;
        if (bindUrl.equals(BuildConfig.BASE_URL)) {
            type = "release : " + bindUrl;
        } else if (bindUrl.equals(BuildConfig.BASE_URL)) {
            type = "pre : " + bindUrl;
        } else if (bindUrl.equals(BuildConfig.BASE_URL)) {
            type = "debug : " + bindUrl;
        }
        return type;
    }
}
